package com.lanou.myapplication.tablelayoutFragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lanou.myapplication.Toplinefragment.Toplinefragment;
import com.lanou.myapplication.educateFragment.educatefragment;
import com.lanou.myapplication.entertainmentFragment.entertainmentfragment;
import com.lanou.myapplication.movieFragment.Moviefragment;
import com.lanou.myapplication.sportsFragment.Sportsfragment;
import com.lanou.myapplication.technologyFragment.Technologyfragment;

/**
 * Created by dllo on 15/10/20.
 */
public class MyFragment extends Fragment {

    private  static  final  String ARGS_POSITION ="position";
    private static SparseArray<Fragment> fragmentSparseArray = new SparseArray<>();

    public static  Fragment newInstance(int position) {
        Fragment f = fragmentSparseArray.get(position);
        if ((f == null)) {
            switch (position) {
                case 0:
                    f = new Toplinefragment();
//                    Bundle bundle_jokes = new Bundle();
//                    bundle_jokes.putInt("position", position);
//                    f.setArguments(bundle_jokes);
                    break;
                case 1:
                  f = new entertainmentfragment();
//                    Bundle bundle_images = new Bundle();
//                    bundle_images.putInt("position", position);
//                    f.setArguments(bundle_images);
                    break;
                case 2:
                    f = new Sportsfragment();
//                    Bundle bundle_images = new Bundle();
//                    bundle_images.putInt("position", position);
//                    f.setArguments(bundle_images);
                    break;


               case 3:
                    f = new Technologyfragment();
////                    Bundle bundle_images = new Bundle();
////                    bundle_images.putInt("position", position);
////                    f.setArguments(bundle_images);
                    break;
                case 4:
                    f = new educatefragment();
//                    Bundle bundle_images = new Bundle();
//                    bundle_images.putInt("position", position);
//                    f.setArguments(bundle_images);
                    break;
                case 5:
                    f = new Moviefragment();
//                    Bundle bundle_images = new Bundle();
//                    bundle_images.putInt("position", position);
//                    f.setArguments(bundle_images);
                    break;

                default:
                    f = new Toplinefragment();
                    break;

            }

            fragmentSparseArray.put(position, f);

        }
        return f;
    }
    /**IdentityHashMap<Integer, Fragment> fragmentSparseArraypr;
     * 上下文
     * */
    private Context context;
    private TextView textView;
    private  int position;




    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }
    /**
     * 在这个方法里面 获取传入的数据
     *
     * */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        position = args.getInt(ARGS_POSITION);
    }

    /**
     * 这个方法里面进行加载布局
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return new TextView(context);
    }


    /**
     * 初始化控件
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        textView = (TextView) view;
    }

    /**
     * 初始化数据
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        textView.setText(position+"");
    }


}

